package com.allen.control;

import com.allen.bean.Outstore;
import com.allen.service.OutstoreService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/outstoreCtrl")
public class OutstoreControl extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String parm = req.getParameter("parm");
        switch (parm) {
            case "list":
                list(req, resp);
                break;
            case "select":
                select(req, resp);
                break;
            case "limit":
                limit(req, resp);
                break;
            case "sub":
                sub(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "detail":
                detail(req, resp);
        }
    }

    private void detail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Outstore outstore = new OutstoreService().detail(req, resp);
        req.setAttribute("outstore",outstore);
        req.getRequestDispatcher("/outstore/detail.jsp").forward(req,resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        new OutstoreService().add(req, resp);
    }

    private void sub(HttpServletRequest req, HttpServletResponse resp) {
        int num = Integer.parseInt(req.getParameter("num"));
        int gid = Integer.parseInt(req.getParameter("gid"));
        int number = new OutstoreService().limit(gid);
        num = number - num;
        new OutstoreService().updateNum(num, gid);
    }

    private void limit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int num = Integer.parseInt(req.getParameter("num"));
        String sgid = req.getParameter("gid");
        if (sgid == null || sgid == "") {
            resp.getWriter().print(-1);
            return;
        }
        int gid = Integer.parseInt(sgid);
        int number = new OutstoreService().limit(gid);
        if (num > number) {
            resp.getWriter().print(number);
        } else {
            resp.getWriter().print(-1);
        }
    }

    private void select(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String json = new OutstoreService().getGoodsJson();
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().print(json);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Outstore> outstores = new OutstoreService().getAllOutstore();
        req.setAttribute("outstores", outstores);
        req.getRequestDispatcher("/outstore/list.jsp").forward(req, resp);
    }
}
